package com.gxuwz.library_provider.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxuwz.commonmodule.entity.Book;;
import com.gxuwz.commonmodule.entity.BorrowRecord;
import com.gxuwz.library_provider.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BookController {
    @Autowired
    private BookMapper bookMapper;


    /**
     * 查询所有图书信息
     * @param book
     * @return
     */
    @GetMapping("/list")
    public List<Book> list(@RequestBody Book book){
        return bookMapper.query(book);
    }

    /**
     * 通过ID查找图书信息
     * @param bookID
     * @return
     */
    @GetMapping("/get/{bookID}")
    public Book getBook(@PathVariable("bookID") String bookID){
        return bookMapper.findByID(bookID);
    }


    /**
     * 添加图书信息
     * @param book
     */
    @PostMapping("/add")
    public void saveBook( @RequestBody Book book)
    {
        bookMapper.insert(book);
    }

    /**
     * 通过ID删除图书信息
     * @param bookID
     */
    @DeleteMapping("/delete/{bookID}")
    public void deleteBook(@PathVariable("bookID") String bookID){
        bookMapper.delete(bookID);
    }

    /**
     * 更新图书信息
     * @param book
     */
    @PutMapping("/update")
    public void editBook(@RequestBody Book book){
        bookMapper.update(book);
    }

    /**
     * 分页（不带条件）
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public PageInfo findPage(@RequestParam(value = "pageIndex") int pageIndex,@RequestParam(value = "pageSize") int pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<Book> books = bookMapper.findPage(null);
        PageInfo<Book> page = new PageInfo<>(books);
        return page;
    }
}
